﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Public.Ocean.Petrel.Extensions.Internal;

namespace Public.Ocean.Petrel.Extensions.UnitTest
{
    [TestClass]
    public class StringConverterTest
    {
        class Constructed
        {
            public string info;
            public Constructed(string info)
            {
                this.info = info;
            }
            public override string ToString()
            {
                return info;
            }
        }

        [TestMethod]
        public void ConvertSingle()
        {
            StringValueConverter converter = new StringValueConverter();
            Assert.AreEqual(42, converter.Unpack<int>(converter.Pack(42)));
            Assert.AreEqual(17.2f, converter.Unpack<float>(converter.Pack(17.2f)));
            Assert.AreEqual("Hi!", converter.Unpack<string>(converter.Pack("Hi!")));
            Assert.AreEqual("Wow", (converter.Unpack<object>(converter.Pack<object>(new Constructed("Wow"))) as Constructed).info);
        }

        [TestMethod]
        public void ConvertArray()
        {
            StringValueConverter converter = new StringValueConverter();
            var data = new[] { 42.1, -19.7 };
            var roundtrip = converter.Unpack<double[]>(converter.Pack(data));
            Assert.AreEqual(data.Length, roundtrip.Length);
            Assert.AreEqual(data[0], roundtrip[0]);
            Assert.AreEqual(data[1], roundtrip[1]);
        }

        [TestMethod]
        public void ConvertNull()
        {
            StringValueConverter converter = new StringValueConverter();
            Assert.IsNull(converter.Unpack<object>(converter.Pack<object>(null)));
            Assert.IsNull(converter.Unpack<object>("Some value"));
            Assert.IsNull(converter.Unpack<object>("clskacsld:cjla"));
        }
    }
}
